package com.cn.data.thread.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author: chenl
 * @Date: 2019/3/3 21:48
 */
public class CyclicBarrierWorker implements Runnable {

    private String serviceName;

    private CyclicBarrier cyclicBarrier;

    /**
     *
     * @param serviceName
     * @param cyclicBarrier
     */
    public CyclicBarrierWorker(String serviceName, CyclicBarrier cyclicBarrier) {
        this.serviceName = serviceName;
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {

        try {
            System.out.println(serviceName + " is waiting");
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

}
